/**************************************************************************************** 
 Copyright © 2003-2012 ZTEsoft Corporation. All rights reserved. Reproduction or       <br>
 transmission in whole or in part, in any form or by any means, electronic, mechanical <br>
 or otherwise, is prohibited without the prior written consent of the copyright owner. <br>
 ****************************************************************************************/
package com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import com.iwhalecloud.bss.kite.client.api.scene.operate.IOperateSceneCommonService;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.client.dto.DataResult;
import com.iwhalecloud.bss.kite.client.vo.scene.CommonAttrVO;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.dto.cust.CuccContextCustDTO;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.enums.InstType;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.service.scene.attr.OperateInstAttrService;
import com.iwhalecloud.bss.kite.web.context.KiteWebContext;
import com.iwhalecloud.bss.kite.web.service.accept.attr.handler.AbstractAttrHandler;

/** 
 * <Description> <br> 
 *  
 * @author tengg<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2021年3月16日 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.cucc.web.service.accept.handler.attr <br>
 */
@Service
public class ApplicationUnitTypeHander extends AbstractAttrHandler {

    @Resource
    private IQuerySceneInstService querySceneInstService;
    
    @Autowired
    private OperateInstAttrService operateInstAttrService;

    @Autowired
    private IOperateSceneCommonService operateSceneCommonService;
    /**
     * Description: <br> 
     *  
     * @author tengg<br>
     * @taskId <br>
     * @param commonAttr <br>
     */ 
    @Override
    public void init(CommonAttrVO commonAttr) {
        // TODO Auto-generated method stub
        //订购之后的场景回填申请单位类型数据信息
        CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
        String sceneInstId = KiteWebContext.getContext().getCurrentSceneInstId();
        List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
        prodInstList.forEach(prodInst ->{
            //申请单位类型
            KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "200001801");
            if (Objects.nonNull(prodAttr) && KiteStringUtils.isNotEmpty(prodAttr.getValue())) {
                commonAttr.setValue(prodAttr.getValue());
                commonAttr.setValueDesc(prodAttr.getValueDesc());
            }
        });
    }

    @Override
    public DataResult<Boolean> apply(String sceneInstId, String fieldName, String value, String valueDesc) {
        //super.apply(sceneInstId, fieldName, value, valueDesc);
        if (KiteStringUtils.isNotEmpty(value)) {
            CuccContextCustDTO custDTO1 = (CuccContextCustDTO) KiteWebContext.getContext().getCust();
            //查询这个offer下有效的接入产品
            List<KiteInst> prodInstList = querySceneInstService.listInsts(sceneInstId, Collections.singletonList(InstType.PROD_INST));
            prodInstList.forEach(prodInst ->{
                //申请单位性质
                KiteAttr prodAttr = querySceneInstService.getProdInstAttr(custDTO1.getCustId(), prodInst.getInstId(), "200001801");
                if (Objects.nonNull(prodAttr) && !KiteStringUtils.isEqual(prodAttr.getValue(), value)) {
                    operateInstAttrService.setAttrValue(prodInst, prodAttr.getFieldName(), value, sceneInstId, null);
                }
            });
            return super.apply(sceneInstId, fieldName, value, valueDesc);
        }
        return ErrorConsts.SUCCESS.getResult(true);
    }
}
